﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Member;
using WuDada.Core.Member.Service;
using WuDada.Core.Member.Domain;
using WuDada.Core.SystemApplications.Service;
using WuDada.Core.SystemApplications;
using WuDada.Core.SystemApplications.Domain;
using System.Text;

public partial class member : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private MemberFactory m_MemberFactory;
    private SystemFactory m_SystemFactory;
    private IMemberService m_MemberService;
    private ICityService m_CityService;
    private string m_FinishUrl = "act-all.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        m_MemberFactory = new MemberFactory();
        m_SystemFactory = new SystemFactory();
        m_MemberService = m_MemberFactory.GetMemberService();
        m_CityService = m_SystemFactory.GetCityService();

        if (!IsPostBack)
        {
            SetControl();
        }
    }

    private void SetControl()
    {
        //縣市
        ddlCity.Items.Clear();
        ddlCity.Items.Add(new ListItem("請選擇", ""));
        IList<CityVO> cityList = m_CityService.GetCityList();
        if (cityList != null && cityList.Count > 0)
        {
            foreach (CityVO city in cityList)
            {
                ddlCity.Items.Add(new ListItem(city.CityName, city.CityName));
            }
        }
    }

    protected void btnSend_Click(object sender, ImageClickEventArgs e)
    {
        MemberVO memberVO = new MemberVO();
        UIHelper.FillVO(UpdatePanel1, memberVO);
        //電子報
        if (rdoEpaperY.Checked)
        {
            memberVO.Epaper = true;
        }
        //FB信箱
        if (rdoFBY.Checked)
        {
            memberVO.FB_Email = txtFB_Email.Text.Trim();
        }
        else
        {
            memberVO.FB_Email = string.Empty;
        }
        //性別
        if (rdoSexF.Checked)
        {
            memberVO.Sex = "男";
        }
        else if (rdoSexM.Checked)
        {
            memberVO.Sex = "女";
        }
        //信箱
        memberVO.Email = memberVO.LoginId;
        //電話
        if (!string.IsNullOrEmpty(txtPhone1_1.Text.Trim()) && !string.IsNullOrEmpty(txtPhone1_2.Text.Trim()))
        {
            memberVO.Phone = txtPhone1_1.Text.Trim() + "-" + txtPhone1_2.Text.Trim();
        }
        //手機
        if (!string.IsNullOrEmpty(txtMobile1_1.Text.Trim()) && !string.IsNullOrEmpty(txtMobile1_2.Text.Trim()))
        {
            memberVO.Mobile = txtMobile1_1.Text.Trim() + "-" + txtMobile1_2.Text.Trim();
        }
        //生日
        memberVO.BirthDay = ucDate.Date;
        //建立與更新時間
        memberVO.CreateTime = DateTime.Now;
        memberVO.UpdateTime = DateTime.Now;

        //判斷是否已經有帳號了
        MemberVO existMember = m_MemberService.GetMemberByLoginId(memberVO.LoginId);
        if (existMember != null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS(string.Format("帳號【{0}】已經註冊過了，請輸入其他帳號。", memberVO.LoginId)), false);
        }
        else
        {
            //判斷必填與錯誤
            if (CheckErrorOK(memberVO))
            {
                m_MemberService.CreateMember(memberVO);
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("感謝您的支持，加入成功。", m_FinishUrl), false);
            }
        }
    }

    private bool CheckErrorOK(MemberVO memberVO)
    {
        bool result = true;
        StringBuilder sbError = new StringBuilder();        

        //姓名必填
        if (string.IsNullOrEmpty(memberVO.Name))
        {
            sbError.Append("請輸入您的姓名。" + "\\n");
        }
        //性別必填
        if (string.IsNullOrEmpty(memberVO.Sex))
        {
            sbError.Append("請選擇您的性別。" + "\\n");
        }
        //生日必填
        if (memberVO.BirthDay == null)
        {
            sbError.Append("請選擇您的生日，或是您選擇的日期是錯誤的。" + "\\n");
        }
        //帳號必填
        if (string.IsNullOrEmpty(memberVO.LoginId))
        {
            sbError.Append("請輸入您的帳號。" + "\\n");
        }
        else
        {
            MemberVO existMemberVO = m_MemberService.GetMemberByLoginId(memberVO.LoginId);
            if (existMemberVO != null)
            {
                sbError.Append("您輸入的帳號已經註冊過了，請重新輸入。" + "\\n");
            }
        }
        //密碼必填
        if (string.IsNullOrEmpty(memberVO.LoginPassword))
        {
            sbError.Append("請輸入您的密碼。" + "\\n");
        }
        else
        {
            if (!memberVO.LoginPassword.Equals(txtCheckLoginPassword.Text.Trim()))
            {
                sbError.Append("您輸入的確認密碼與您的密碼不相同，請重新輸入。" + "\\n");
            }
        }
        //手機必填
        if (string.IsNullOrEmpty(memberVO.Mobile))
        {
            sbError.Append("請輸入您的行動電話。" + "\\n");
        }
        //縣市必填
        if (string.IsNullOrEmpty(memberVO.City))
        {
            sbError.Append("請選擇您所在的縣市。" + "\\n");
        }

        if (!string.IsNullOrEmpty(sbError.ToString()))
        {
            result = false;
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS(sbError.ToString()), false);
        }        

        return result;
    }
}